﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Threading;
using System.Reflection;

namespace AutomatedTP
{

    /// <summary>
    /// Main class for whole program. Call other classes and define order of operations.
    /// </summary>
    class Program
    {

        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The input arguments from command line.</param>
        static void Main(string[] args)
        {
            Logger.setPath();
            Logger.WriteToLog(3, "");
            Logger.WriteToLog(3, "Process started.");  

            var options = new Options();
            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {                
                Logger.setLevel(options.Log);
                
                if (options.help)
                {
                    Logger.WriteToLog(2, "DONE");
                    return;
                }

                if (options.Items == null)
                {
                    Console.WriteLine("You must specify source test cases\n\n" + options.GetUsage());
                    Logger.WriteToLog(3, "You must specify source test cases");
                    return;
                }
            }
            else
            {                
                return;
            }

            Logger.verbose = options.verbose;            
            Logger.WriteToLog(3, "Parsing command line parameters finished.");            

            TP tp_one = new TP();            

            tp_one.SetLevel(options.Coverage);
                        
            tp_one.SetManual(options.Manual);

            //future!
            tp_one.SetTiming(false);

            Parse parse = new Parse();

            //Extract module information from XML files
            if (parse.parse_xml(options, ref tp_one) == 1)
                return;

            //path will be parametrized                       
            String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            String fileName = tp_one.GetName();
            tp_one.SetName(fileName);
            fileName += ".c";
            String pathString = Path.Combine(path, fileName);                

            if (System.IO.File.Exists(pathString))
            {                     
                Logger.WriteToLog(2, pathString);
                Logger.WriteToLog(2, "File for TP exist!");
            }
            else
            { 
                Logger.WriteToLog(3, "File for TP does not exist and will be created!");
                File.Create(pathString).Close();
            }
            
            Writer writer = new Writer(fileName);
            using (StreamWriter file = new System.IO.StreamWriter(pathString))
            {                
                //write whole TP
                writer.print_tp(file, tp_one);
                
                //create and write header
                writer.print_header(path, tp_one);
                
                //create and write ipg file
                writer.print_ipg(path, tp_one);

                //create and write ipg_cc file
                writer.print_ipg_cc(path, tp_one);                                
            }            

            //============================================================            
            Logger.WriteToLog(3, "PROCESS FINISHED");            
        }        
    }
}
